// MainWindow.cs
//
//  Copyright (C) 2009 Whitemice Consulting
//
// This is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.
// 
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this software; if not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.
//
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Gtk;
using Whitemice.GtkWidgets;
using Whitemice.ZOGI;

public partial class MainWindow: Gtk.Window, IZOGIWidget
{
  MultiResultCallback callback;
  
  public MainWindow (): base (Gtk.WindowType.Toplevel)
  {
    Build ();
    callback = new MultiResultCallback(this.ReceiveDataCallback);
  }
  
  protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  {
    Application.Quit ();
    a.RetVal = true;
  }

  public void ReceiveRPCCall(RPCEventArgs args)
  {
    Console.WriteLine("MainWindow.ReceiveRPCCall");
    if (args.IsEmpty) Console.WriteLine("Payload Empty.");
    if (args.IsError) Console.WriteLine("RPC Error.");
    if (args.HasResults)
    {
      StringBuilder text = new StringBuilder();
      foreach(Entity e in args.Payload)
      {
        if (text.Length > 0)
          text.Append(", ");
        text.Append(e.ObjectId.ToString());
      }
      display.Text = text.ToString();
    } else display.Text = "No results!";
  }

  protected virtual void OnFetchClicked (object sender, System.EventArgs e)
  {
    

    display.Text = "Searching...";

    
    ProjectSearchCommand command;
    command = new ProjectSearchCommand("CIS:", null, 65535, "Project");
	  command.Callback = callback;
		command.ANDMode();

    /*
    ContactSearchCommand command;
    
    command = new ContactSearchCommand("Search");
	  command.AddANDCriteria("firstName", "Adam", false, false);
	  command.AddORCriteria("phone.number", "616*", true, false);
	  command.Callback = callback;
	  */
	  Server.Connection.RunCommand(command);
  }
}